import { Injectable } from '@nestjs/common'
import { PassportStrategy } from '@nestjs/passport'
import { Strategy, ExtractJwt } from 'passport-jwt'
import { Request } from 'express'
import { Payload } from 'src/module/login/login.interface'
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: 'jwtConstants.secret',
      passReqToCallback: true, //设置回调的第一个参数是  request
    })
  }
  async validate(request: Request, payload: Payload) {

    // const { userId, pv } = payload
    // const token = (request.headers as any).authorization.slice(7)
    // await this.authService.validateToken(userId, pv, token)
    // return { userId } //返回值会被 守卫的  handleRequest方法 捕获
  }
  /* 主动处理错误 */
  // handleRequest(err, user, info) {
  //   if (err || !user) {
  //     throw err || new ApiException('登录状态已过期', 401)
  //   }
  //   return user
  // }
}
